// Decompiled by Jad v1.5.8f. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   PatrocinanteDAO.java

package org.fiel.admin.dao;

import java.io.PrintStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.fiel.admin.struts.forms.PatrocinanteForm;
import org.fiel.domain.Patrocinante;

public class PatrocinanteDAO
{

    public PatrocinanteDAO()
        throws Exception
    {
        conn = null;
        try
        {
            Context ctx = new InitialContext();
            DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/FielDB");
            conn = ds.getConnection();
        }
        catch(Exception e)
        {
            System.err.println("PatrocinanteDAO() Error:\n" + e.getMessage());
            throw new Exception("PatrocinanteDAO() Error:\n" + e.getMessage());
        }
    }

    public void createPatrocinante(PatrocinanteForm patrocinante)
        throws Exception
    {
        PreparedStatement pstmt = null;
        try
        {
            conn.setAutoCommit(false);
            pstmt = conn.prepareStatement("INSERT INTO patrocinantes (descripcion) VALUES(?)");
            pstmt.setString(1, patrocinante.getPatrocinante());
            pstmt.executeUpdate();
            conn.commit();
            conn.setAutoCommit(true);
        }
        catch(Exception e)
        {
            conn.rollback();
            System.err.println("PatrocinanteDAO.createPatrocinante() Error:\n" + e.getMessage());
            throw new Exception("PatrocinanteDAO.createPatrocinante() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
    }

    public List retrievePatrocinantes()
        throws Exception
    {
        PreparedStatement pstmt = null;
        ResultSet rset = null;
        ArrayList patrocinantes = new ArrayList();
        try
        {
            pstmt = conn.prepareStatement("SELECT * FROM patrocinantes ORDER BY descripcion ASC");
            Patrocinante patrocinante;
            for(rset = pstmt.executeQuery(); rset.next(); patrocinantes.add(patrocinante))
            {
                patrocinante = new Patrocinante();
                patrocinante.setId(Integer.valueOf(rset.getString("id")));
                patrocinante.setDescripcion(rset.getString("descripcion"));
            }

        }
        catch(Exception e)
        {
            System.err.println("PatrocinanteDAO.retrievePatrocinantes() Error:\n" + e.getMessage());
            throw new Exception("PatrocinanteDAO.retrievePatrocinantes() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(rset != null)
                    rset.close();
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
        return patrocinantes;
    }

    public List retrievePatrocinantesByDescripcion(String descripcion)
        throws Exception
    {
        Statement stmt = null;
        ResultSet rset = null;
        ArrayList patrocinantes = new ArrayList();
        try
        {
            stmt = conn.createStatement();
            Patrocinante patrocinante;
            for(rset = stmt.executeQuery("SELECT * FROM patrocinantes WHERE descripcion LIKE \"%" + descripcion + "%\" ORDER BY descripcion"); rset.next(); patrocinantes.add(patrocinante))
            {
                patrocinante = new Patrocinante();
                patrocinante.setId(Integer.valueOf(rset.getString("id")));
                patrocinante.setDescripcion(rset.getString("descripcion"));
            }

        }
        catch(Exception e)
        {
            System.err.println("PatrocinanteDAO.retrievePatrocinantesByDescripcion() Error:\n" + e.getMessage());
            throw new Exception("PatrocinanteDAO.retrievePatrocinantesByDescripcion() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(rset != null)
                    rset.close();
                if(stmt != null)
                    stmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
        return patrocinantes;
    }

    public Patrocinante retrievePatrocinante(String id)
        throws Exception
    {
        PreparedStatement pstmt = null;
        ResultSet rset = null;
        Patrocinante patrocinante = null;
        try
        {
            pstmt = conn.prepareStatement("SELECT * FROM patrocinantes WHERE id = ?");
            pstmt.setString(1, id);
            rset = pstmt.executeQuery();
            if(rset.next())
            {
                patrocinante = new Patrocinante();
                patrocinante.setId(Integer.valueOf(rset.getString("id")));
                patrocinante.setDescripcion(rset.getString("descripcion"));
            }
        }
        catch(Exception e)
        {
            System.err.println("PatrocinanteDAO.retrievePatrocinante() Error:\n" + e.getMessage());
            throw new Exception("PatrocinanteDAO.retrievePatrocinante() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(rset != null)
                    rset.close();
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
        return patrocinante;
    }

    public void updatePatrocinante(PatrocinanteForm patrocinante)
        throws Exception
    {
        PreparedStatement pstmt = null;
        try
        {
            conn.setAutoCommit(false);
            pstmt = conn.prepareStatement("UPDATE patrocinantes SET descripcion = ? WHERE id = ?");
            pstmt.setString(1, patrocinante.getPatrocinante());
            pstmt.setString(2, patrocinante.getId());
            pstmt.executeUpdate();
            conn.commit();
            conn.setAutoCommit(true);
        }
        catch(Exception e)
        {
            conn.rollback();
            System.err.println("PatrocinanteDAO.updatePatrocinante() Error:\n" + e.getMessage());
            throw new Exception("PatrocinanteDAO.updatePatrocinante() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
    }

    public void deletePatrocinante(String id)
        throws Exception
    {
        PreparedStatement pstmt = null;
        try
        {
            conn.setAutoCommit(false);
            pstmt = conn.prepareStatement("DELETE FROM patrocinantes WHERE id = ?");
            pstmt.setString(1, id);
            pstmt.executeUpdate();
            conn.commit();
            conn.setAutoCommit(true);
        }
        catch(Exception e)
        {
            conn.rollback();
            System.err.println("PatrocinanteDAO.deletePatrocinante() Error:\n" + e.getMessage());
            throw new Exception("PatrocinanteDAO.deletePatrocinante() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
    }

    private Connection conn;
}
